const borrowingsModule = {
    // 加载借阅记录
    loadBorrowings() {
        const borrowings = dataModule.getBorrowings();
        this.updateStats(borrowings);
        this.displayBorrowings(borrowings);
        this.initializeFilters();
    },

    // 更新统计数据
    updateStats(borrowings) {
        const currentBorrowings = borrowings.filter(b => !b.returnDate).length;
        const totalBorrowings = borrowings.length;

        document.getElementById('current-borrowings').textContent = currentBorrowings;
        document.getElementById('total-borrowings').textContent = totalBorrowings;
    },

    // 显示借阅记录
    displayBorrowings(borrowings) {
        const container = document.getElementById('borrowings-container');
        container.innerHTML = '';

        borrowings.forEach(borrowing => {
            const card = this.createBorrowingCard(borrowing);
            container.appendChild(card);
        });
    },

    // 创建借阅记录卡片
    createBorrowingCard(borrowing) {
        const card = document.createElement('div');
        card.className = 'borrowing-card';
        
        const book = dataModule.getBookById(borrowing.bookId);
        const status = borrowing.returnDate ? 'returned' : 'active';
        const statusText = borrowing.returnDate ? '已归还' : '借阅中';
        const bookIcon = this.getBookIcon(book.category);
        const categoryColor = this.getCategoryColor(book.category);
        
        card.innerHTML = `
            <div class="book-cover" style="background: ${categoryColor}">
                <div class="book-icon-wrapper">
                    <i class="fas ${bookIcon}"></i>
                    <span class="category-label">${book.category}</span>
                </div>
            </div>
            <div class="borrowing-info">
                <div class="book-title">${book.title}</div>
                <div class="borrowing-meta">
                    <div class="meta-item">
                        <i class="fas fa-user"></i>
                        <span>${book.author}</span>
                    </div>
                    <div class="meta-item">
                        <i class="fas fa-calendar"></i>
                        <span>借阅日期：${borrowing.borrowDate}</span>
                    </div>
                    ${borrowing.returnDate ? `
                    <div class="meta-item">
                        <i class="fas fa-calendar-check"></i>
                        <span>归还日期：${borrowing.returnDate}</span>
                    </div>
                    ` : ''}
                </div>
                <span class="borrowing-status ${status}">${statusText}</span>
                ${!borrowing.returnDate ? `
                <button class="return-btn" onclick="borrowingsModule.returnBook(${borrowing.id})">
                    <i class="fas fa-undo"></i>
                    归还
                </button>
                ` : ''}
            </div>
        `;
        return card;
    },

    // 根据分类获取对应的图标
    getBookIcon(category) {
        const icons = {
            '文学': 'fa-book-open',
            '科幻': 'fa-rocket',
            '计算机': 'fa-laptop-code',
            'default': 'fa-book'
        };
        return icons[category] || icons.default;
    },

    // 根据分类获取对应的颜色
    getCategoryColor(category) {
        const colors = {
            '文学': 'linear-gradient(135deg, #1890ff 0%, #36cfc9 100%)',
            '科幻': 'linear-gradient(135deg, #722ed1 0%, #f759ab 100%)',
            '计算机': 'linear-gradient(135deg, #fa8c16 0%, #faad14 100%)',
            'default': 'linear-gradient(135deg, #1890ff 0%, #36cfc9 100%)'
        };
        return colors[category] || colors.default;
    },

    // 初始化筛选和搜索功能
    initializeFilters() {
        const searchInput = document.getElementById('search-input');
        const statusFilter = document.getElementById('status-filter');

        const filterBorrowings = () => {
            const searchTerm = searchInput.value.toLowerCase();
            const status = statusFilter.value;

            const borrowings = dataModule.getBorrowings().filter(borrowing => {
                const book = dataModule.getBookById(borrowing.bookId);
                const matchesSearch = book.title.toLowerCase().includes(searchTerm);
                const matchesStatus = !status || 
                    (status === '借阅中' && !borrowing.returnDate) ||
                    (status === '已归还' && borrowing.returnDate);

                return matchesSearch && matchesStatus;
            });

            this.displayBorrowings(borrowings);
        };

        // 添加事件监听
        searchInput.addEventListener('input', filterBorrowings);
        statusFilter.addEventListener('change', filterBorrowings);
    },

    // 归还图书
    returnBook(borrowingId) {
        const borrowing = dataModule.getBorrowingById(borrowingId);
        if (borrowing && !borrowing.returnDate) {
            borrowing.returnDate = new Date().toLocaleDateString();
            const book = dataModule.getBookById(borrowing.bookId);
            book.status = '可借阅';
            this.loadBorrowings();
            alert(`成功归还《${book.title}》`);
        }
    }
};

// 页面加载时初始化
document.addEventListener('DOMContentLoaded', () => {
    borrowingsModule.loadBorrowings();
}); 